﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MiniCAP.AspNetCore
{
    public static class ServiceCollectionExtensions
    {
        public static IServiceCollection AddMiniCap(this IServiceCollection services, Action<MiniCapOptions> configure)
        {
            var options = new MiniCapOptions();
            configure(options);

            services.AddSingleton<IMessageStorage>(new MySqlMessageStorage(options.MySqlConnectionString));
            services.AddSingleton<IMessageTransport>(new RabbitMqTransport(options.RabbitMqConnectionString));
            services.AddScoped<IMessagePublisher, MessagePublisher>();

            // 启动后台服务处理重试和清理  
            services.AddHostedService<MessageProcessorService>();

            return services;
        }
    }

    public class MiniCapOptions
    {
        public string MySqlConnectionString { get; set; }
        public string RabbitMqConnectionString { get; set; }
    }
}
